package com.example.imtoken.core.logic.repository

import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.core.http.ApiException
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.entitys.http.CreateEntity
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AccountSchema
import com.example.imtoken.room.schema.AppLoginSchema
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext

class WalletRepository {

    suspend fun getWalletFind2(
        coin: String?,
        mnemonic: String? = null,
        keystore: String? = null,
        private: String? = null,
        password: String?,
        hint: String? = null
    ) = withContext(IO) {
        val find = RetrofitUtils.apiService?.walletFind2(
            coin,
            mnemonic,
            keystore,
            private,
            password,
            hint
        )
        if (find?.code == 0) {
            if (find.data != null) {
                LogUtils.json(find.data)
                return@withContext find.data
            } else {
                throw ApiException(find.msg)
            }
        } else {
            throw ApiException(find?.msg)
        }
    }

    suspend fun getCoins(coin: String) = withContext(IO) {
        val coins = RetrofitUtils.apiService?.coins2(coin)
        if (coins?.code == 0) {
            if (coins.data != null) {
                LogUtils.json(coins.data)
                return@withContext coins.data
            } else {
                throw ApiException(coins.msg)
            }
        } else {
            throw ApiException(coins?.msg)
        }
    }

    /**
     * 创建用户钱包
     * @param coin String 钱包类型
     * @param passWord String   钱包密码
     * @param realName String   用户名称
     * @param hint String?  密码提示
     */
    suspend fun createUser(
        coin: String,
        passWord: String?,
        realName: String?,
        hint: String? = null,
        token: String? = null
    ) =
        withContext(IO) {
            val create = RetrofitUtils.apiService?.walletCreate2(coin, passWord, realName, hint)
            if (create?.code == 0) {
                if (create.data != null) {
                    LogUtils.json(create.data)
                    return@withContext create.data
                } else {
                    throw ApiException(create.msg)
                }
            } else {
                throw ApiException(create?.msg)
            }
        }

    /**
     * 给钱包绑定推广码
     * @param walletAddress String?
     * @param walletPassword String?
     * @param walletCoin String
     * @param synchronizeSign String?
     * @param token String?
     * @return Any?
     */
    suspend fun bindingWallet(
        walletAddress: String?,
        walletPassword: String?,
        walletCoin: String,
        synchronizeSign: String?,
        token: String? = null
    ) = RetrofitUtils.apiService?.bindingWallet(
        walletAddress,
        walletPassword,
        walletCoin,
        synchronizeSign,
        token
    )

    fun addAccountData(
        createEntity: CreateEntity?,
        passWord: String?,
        realName: String?,
        hint: String? = null
    ) {
        createEntity?.forEach {
            val account = AccountSchema(
                realName,
                passWord,
                hint,
                it.token,
                it.coin?.address,
                it.coin?.coin,
                it.coin?.mnemonic,
                it.coin?.public_key,
                it.coin?.coin,
                1
            )
            if (AppDataBase.instance.getAccountDao().insertAccount(account) > 0) {
                LogUtils.e("钱包账号添加成功")
            } else {
                LogUtils.e("钱包账号添加失败")
            }
        }
    }

    fun addAppLoginData(address: String?) {
        val appLogin = AppLoginSchema(address)
        if (AppDataBase.instance.getAppLoginDao().insertAppLogin(appLogin) > 0) {
            LogUtils.e("app登录标识添加成功")
        } else {
            LogUtils.e("app登录标识添加失败")
        }
    }
}